package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.minio.MinIoTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.thread.UserThreadLoacl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private MinIoTemplate minIoTemplate;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Short isCollection = dto.getIsCollection();
        Integer currentPage = dto.getPage();
        Integer size = dto.getSize();
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmMaterial::getIsCollection,isCollection);

        Page<WmMaterial> page = new Page<>(currentPage,size);
        Page<WmMaterial> pageResult = wmMaterialMapper.selectPage(page, queryWrapper);
        Long total = pageResult.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(currentPage,size,total.intValue());
        pageResponseResult.setData(pageResult.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult upload(MultipartFile multipartFile) throws IOException {
        InputStream inputStream = multipartFile.getInputStream();
        String contentType = multipartFile.getContentType();
        if (StringUtils.isEmpty(contentType)){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        String[] split = contentType.split("/");
        String suffix = split[1];
        String url = minIoTemplate.uploadFile("", System.currentTimeMillis() + "." + suffix, inputStream, "image");
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(UserThreadLoacl.get().getId());
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setIsCollection((short)0);
        wmMaterial.setType((short)0);
        wmMaterial.setUrl(url);
        int insert = wmMaterialMapper.insert(wmMaterial);
        if (insert < 1){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.okResult(wmMaterial);
    }
}
